package com.ruoyi.web.controller.other;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.company.domain.HouseCompanyT;
import com.ruoyi.company.service.IHouseCompanyTService;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 施工企业Controller
 * 
 * @author ruoyi
 * @date 2023-05-23
 */
@RestController
@RequestMapping("/other/company")
public class HouseCompanyTController extends BaseController
{
    @Autowired
    private IHouseCompanyTService houseCompanyTService;
    @Autowired
    private ISysUserService userService;

    /**
     * 查询施工企业列表
     */
    @GetMapping("/list")
    public TableDataInfo list(HouseCompanyT houseCompanyT)
    {
        startPage();
        List<HouseCompanyT> list = houseCompanyTService.selectHouseCompanyTList(houseCompanyT);
        return getDataTable(list);
    }

    /**
     * 导出施工企业列表
     */
    @Log(title = "施工企业", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, HouseCompanyT houseCompanyT)
    {
        List<HouseCompanyT> list = houseCompanyTService.selectHouseCompanyTList(houseCompanyT);
        ExcelUtil<HouseCompanyT> util = new ExcelUtil<HouseCompanyT>(HouseCompanyT.class);
        util.exportExcel(response, list, "施工企业数据");
    }

    /**
     * 获取施工企业详细信息
     */
    @GetMapping(value = "/{companyId}")
    public AjaxResult getInfo(@PathVariable("companyId") Long companyId)
    {
        return AjaxResult.success(houseCompanyTService.selectHouseCompanyTByCompanyId(companyId));
    }

    /**
     * 新增施工企业
     */
    @Log(title = "施工企业", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody HouseCompanyT houseCompanyT)
    {
        if (UserConstants.NOT_UNIQUE.equals(userService.checkUserNameUnique(houseCompanyT.getUserName())))
        {
            return AjaxResult.error("新增用户'" + houseCompanyT.getUserName() + "'失败，登录账号已存在");
        }
        HouseCompanyT vo=new HouseCompanyT();
        SysUser user=new SysUser();
        user.setCreateBy(getUsername());
        user.setPassword(SecurityUtils.encryptPassword(houseCompanyT.getPassword()));
        user.setUserName(houseCompanyT.getCompanyName());
        user.setNickName(houseCompanyT.getCompanyName());
        user.setRoleId(103L);
        userService.insertUser(user);
        houseCompanyT.setCreater(getUsername());
        return toAjax(houseCompanyTService.insertHouseCompanyT(houseCompanyT));
    }

    /**
     * 修改施工企业
     */
    @Log(title = "施工企业", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody HouseCompanyT houseCompanyT)
    {
        return toAjax(houseCompanyTService.updateHouseCompanyT(houseCompanyT));
    }

    /**
     * 删除施工企业
     */
    @Log(title = "施工企业", businessType = BusinessType.DELETE)
	@DeleteMapping("/{companyIds}")
    public AjaxResult remove(@PathVariable Long[] companyIds)
    {
        return toAjax(houseCompanyTService.deleteHouseCompanyTByCompanyIds(companyIds));
    }
}
